import requests
import re
from .spider import Spider
'''
https://www.qidian.com/free/all
https://book.qidian.com/info/1005635953
<a href="//book.qidian.com/info/1005635953" data-bid="1005635953" data-eid="qd_B57" target="_blank"><img src="//bookcover.yuewen.com/qdbimg/349573/1005635953/150"></a>
<h4><a href="//book.qidian.com/info/1012216956" target="_blank" data-eid="qd_B58" data-bid="1012216956">我一个人砍翻末世</a></h4>
 
<span class="blue">连载</span>

<a class="writer" href="//me.qidian.com/authorIndex.aspx?id=4921" target="_blank" data-eid="qd_G08">唐家三少</a>
<a href="//book.qidian.com/info/1005635953">大龟甲师</a>
orderId=&vip=hidden&style=1&pageSize=20&siteid=1&pubflag=0&hiddenField=1&page=1

'''
class Novel:
    header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'
              }
    novel_url = 'https://book.qidian.com/info/'
    def __init__(self,
                 page = '1',
                 url ='https://www.qidian.com/free/all',
                 charset = 'utf-8',
                 ):
        self.page = page
        self.url = url
        self.charset = charset
        self.text = self.free_text()
        self.free_info = self.free_info()
    def free_text(self):
        params = {'orderId':'',
                  'vip':'hidden',
                  'style':'1',
                  'pageSize':'20',
                  'siteid':'1',
                  'pubflag':'0',
                  'hiddenField':'1',
                  'page':'{}'.format(self.page)}
        res = requests.get(self.url,params = params,headers = self.header)
        res.encoding = self.charset
        
        return res.text
    def free_info(self):
        urlid_name = '<h4><a href=".*?" target="_blank" data-eid="qd_B58" data-bid="(.*?)">(.*?)</a></h4>'
        img = '<a href=".*?" data-bid=".*?" data-eid="qd_B57" target="_blank"><img src="(.*?)"></a>'
        
        urlid_name = re.findall(urlid_name,self.text)
        img = re.findall(img,self.text)
        info_dict = {}
        for i in range(len(img)):
            info_dict[urlid_name[i][1]] = {'book_id':urlid_name[i][0],
                                           'img':img[i]}
        return info_dict
    
        
            
if __name__ =='__main__':
    n = Novel().every_book()
